// var $storageObj = require("./AL_Framework_Storage_Obj");
// var a = (function () {
//     function e() {
//         this.logTag = "[AL_Framework_Local_Manager]";
//         this.key = "VuxiAKihQ0VR9WRe";
//         this.map_storage = new Map();
//         this.class_Manager = new s();
//     }
//     e.Instance = function () {
//         return this._instance || (this._instance = new e());
//     };
//     e.prototype.register = function (e) {
//         if (0 == this.map_storage.has(e)) {
//             var t = new $storageObj.AL_Framework_Storage_Obj();
//             t.shezhi_cunchu_qianzhui(e);
//             t.shezhi_key(this.key);
//             this.map_storage.set(e, t);
//         } else {
//             Log.w("存储器[" + e + "]已注册，请检查是否有同名bundle!");
//         }
//     };
//     e.prototype.shezhi_key = function (e) {
//         this.key = e;
//         this.map_storage.forEach(function (t) {
//             t.shezhi_key(e);
//         });
//     };
//     e.prototype.huoqu_cunchu_duixiang = function (e) {
//         if (!this.map_storage.has(e)) {
//             this.register(e);
//         }
//         return this.map_storage.get(e);
//     };
//     e.prototype.setItem = function (e, t, o, i) {
//         if (this.jiancha_cunchu_duixiang_shifou_cunzai(e)) {
//             this.map_storage.get(e).setItem(t, o, i);
//         }
//     };
//     e.prototype.getItem = function (e, t, o) {
//         if (void 0 === o) {
//             o = null;
//         }
//         return this.jiancha_cunchu_duixiang_shifou_cunzai(e) ? this.map_storage.get(e).getItem(t, o) : o;
//     };
//     e.prototype.removeItem = function (e, t) {
//         if (this.jiancha_cunchu_duixiang_shifou_cunzai(e)) {
//             this.map_storage.get(e).removeItem(t);
//         }
//     };
//     e.prototype.qingchu_suoyou = function () {
//         localStorage.clear();
//     };
//     e.prototype.zhuce_lei = function (e) {
//         this.class_Manager.register(e);
//     };
//     e.prototype.huoqu_cunchu_lei = function () {
//         var e = arguments;
//         return "string" == typeof e[0]
//             ? (this.jiancha_cunchu_duixiang_shifou_cunzai(e[0]), this.class_Manager.huoqu_chuliqi_by_key(e[0], e[1]))
//             : (this.jiancha_cunchu_duixiang_shifou_cunzai(e[0].bundle), this.class_Manager.huoqu_chuliqi(e[0]));
//     };
//     e.prototype.jiancha_cunchu_duixiang_shifou_cunzai = function (e) {
//         return !!this.map_storage.has(e) || (this.register(e), !0);
//     };
//     e.prototype.huoqu_suoyou_yuancheng_key = function () {
//         var e = [];
//         this.map_storage.forEach(function (t) {
//             e = __spreadArrays(e, t.huoqu_yuancheng_key_list());
//         });
//         return e;
//     };
//     e._instance = null;
//     return e;
// })();
// exports.default = a;
// var s = (function () {
//     function e() {
//         this.handlerMap = new Map();
//     }
//     e.prototype.register = function (e) {
//         var t = e.bundle;
//         var o = e.key;
//         var i = t + "_" + o;
//         if (this.handlerMap.has(i)) {
//             return this.handlerMap.get(i);
//         }
//         var n = new e(t, o);
//         this.handlerMap.set(i, n);
//         return n;
//     };
//     e.prototype.huoqu_chuliqi = function (e) {
//         var t = e.bundle + "_" + e.key;
//         return this.handlerMap.has(t) ? this.handlerMap.get(t) : this.register(e);
//     };
//     e.prototype.huoqu_chuliqi_by_key = function (e, t) {
//         var o = e + "_" + t;
//         return this.handlerMap.has(o) ? this.handlerMap.get(o) : null;
//     };
//     return e;
// })();


// 引入StorageObj模块
const $storageObj = require("./AL_Framework_Storage_Obj");

/** 
 * @class AL_Framework_Local_Manager
 * @description 管理本地存储的单例类 
 */
class AL_Framework_Local_Manager {
    constructor() {
        /** @type {string} logTag 日志标签 */
        this.logTag = "[AL_Framework_Local_Manager]";
        /** @type {string} key 存储的密钥 */
        this.key = "VuxiAKihQ0VR9WRe";
        /** @type {Map<string, AL_Framework_Storage_Obj>} map_storage 存储对象的Map */
        this.map_storage = new Map();
        /** @type {ClassManager} class_Manager 类管理器 */
        this.class_Manager = new ClassManager();
    }

    /** 
     * @function Instance
     * @description 获取单例实例
     * @returns {AL_Framework_Local_Manager} 
     */
    static Instance() {
        // 如果实例不存在则创建一个新的实例
        return this._instance || (this._instance = new AL_Framework_Local_Manager());
    }

    /**
     * @function register
     * @description 注册存储对象
     * @param {string} key 存储对象的键
     */
    register(key) {
        if (!this.map_storage.has(key)) {
            const storageObj = new $storageObj.AL_Framework_Storage_Obj();
            storageObj.shezhi_cunchu_qianzhui(key);
            storageObj.shezhi_key(this.key);
            this.map_storage.set(key, storageObj);
        } else {
            console.warn(`存储器[${key}]已注册，请检查是否有同名bundle!`);
        }
    }

    /**
     * @function shezhi_key
     * @description 设置存储对象的密钥
     * @param {string} key 新的密钥
     */
    shezhi_key(key) {
        this.key = key;
        this.map_storage.forEach((storageObj) => {
            storageObj.shezhi_key(key);
        });
    }

    /**
     * @function huoqu_cunchu_duixiang
     * @description 获取存储对象
     * @param {string} key 存储对象的键
     * @returns {AL_Framework_Storage_Obj}
     */
    huoqu_cunchu_duixiang(key) {
        if (!this.map_storage.has(key)) {
            this.register(key);
        }
        return this.map_storage.get(key);
    }

    /**
     * @function setItem
     * @description 设置存储项
     * @param {string} key 存储对象的键
     * @param {string} itemKey 项目键
     * @param {any} value 项目值
     * @param {number} [type] 项目类型
     */
    setItem(key, itemKey, value, type) {
        if (this.jiancha_cunchu_duixiang_shifou_cunzai(key)) {
            this.map_storage.get(key).setItem(itemKey, value, type);
        }
    }

    /**
     * @function getItem
     * @description 获取存储项
     * @param {string} key 存储对象的键
     * @param {string} itemKey 项目键
     * @param {any} [defaultValue=null] 默认值
     * @returns {any}
     */
    getItem(key, itemKey, defaultValue = null) {
        return this.jiancha_cunchu_duixiang_shifou_cunzai(key) ? this.map_storage.get(key).getItem(itemKey, defaultValue) : defaultValue;
    }

    /**
     * @function removeItem
     * @description 移除存储项
     * @param {string} key 存储对象的键
     * @param {string} itemKey 项目键
     */
    removeItem(key, itemKey) {
        if (this.jiancha_cunchu_duixiang_shifou_cunzai(key)) {
            this.map_storage.get(key).removeItem(itemKey);
        }
    }

    /**
     * @function qingchu_suoyou
     * @description 清除所有存储项
     */
    qingchu_suoyou() {
        localStorage.clear();
    }

    /**
     * @function zhuce_lei
     * @description 注册类
     * @param {any} classObj 类对象
     */
    zhuce_lei(classObj) {
        this.class_Manager.register(classObj);
    }

    /**
     * @function huoqu_cunchu_lei
     * @description 获取存储类
     * @returns {any}
     */
    huoqu_cunchu_lei() {
        const args = arguments;
        return typeof args[0] === "string"
            ? (this.jiancha_cunchu_duixiang_shifou_cunzai(args[0]), this.class_Manager.huoqu_chuliqi_by_key(args[0], args[1]))
            : (this.jiancha_cunchu_duixiang_shifou_cunzai(args[0].bundle), this.class_Manager.huoqu_chuliqi(args[0]));
    }

    /**
     * @function jiancha_cunchu_duixiang_shifou_cunzai
     * @description 检查存储对象是否存在
     * @param {string} key 存储对象的键
     * @returns {boolean}
     */
    jiancha_cunchu_duixiang_shifou_cunzai(key) {
        return this.map_storage.has(key) || (this.register(key), true);
    }

    /**
     * @function huoqu_suoyou_yuancheng_key
     * @description 获取所有远程键
     * @returns {Array<string>}
     */
    huoqu_suoyou_yuancheng_key() {
        let remoteKeys = [];
        this.map_storage.forEach((storageObj) => {
            remoteKeys = [...remoteKeys, ...storageObj.huoqu_yuancheng_key_list()];
        });
        return remoteKeys;
    }
}

// 初始化单例实例
AL_Framework_Local_Manager._instance = null;

// 导出LocalManager
exports.default = AL_Framework_Local_Manager;

/** 
 * @class ClassManager
 * @description 管理类注册与获取的类 
 */
class ClassManager {
    constructor() {
        /** @type {Map<string, any>} handlerMap 处理器Map */
        this.handlerMap = new Map();
    }

    /**
     * @function register
     * @description 注册类处理器
     * @param {any} classObj 类对象
     * @returns {any}
     */
    register(classObj) {
        const bundle = classObj.bundle;
        const key = classObj.key;
        const handlerKey = `${bundle}_${key}`;

        if (this.handlerMap.has(handlerKey)) {
            return this.handlerMap.get(handlerKey);
        }
        const handler = new classObj(bundle, key);
        this.handlerMap.set(handlerKey, handler);
        return handler;
    }

    /**
     * @function huoqu_chuliqi
     * @description 获取处理器
     * @param {any} classObj 类对象
     * @returns {any}
     */
    huoqu_chuliqi(classObj) {
        const handlerKey = `${classObj.bundle}_${classObj.key}`;
        return this.handlerMap.has(handlerKey) ? this.handlerMap.get(handlerKey) : this.register(classObj);
    }

    /**
     * @function huoqu_chuliqi_by_key
     * @description 根据键获取处理器
     * @param {string} bundle 包名
     * @param {string} key 键
     * @returns {any}
     */
    huoqu_chuliqi_by_key(bundle, key) {
        const handlerKey = `${bundle}_${key}`;
        return this.handlerMap.has(handlerKey) ? this.handlerMap.get(handlerKey) : null;
    }
}
